package com.ruoyi.mng.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mng.domain.HzChaChannel;
import com.ruoyi.mng.domain.HzMerBusiR;
import com.ruoyi.mng.service.IHzBusiService;
import com.ruoyi.mng.service.IHzChaChannelService;
import com.ruoyi.mng.service.IHzMerMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mng.mapper.HzChaBusiRMapper;
import com.ruoyi.mng.domain.HzChaBusiR;
import com.ruoyi.mng.service.IHzChaBusiRService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 渠道业务关系Service业务层处理
 *
 * @author chenqiang
 * @date 2023-04-28
 */
@Service
public class HzChaBusiRServiceImpl implements IHzChaBusiRService {
    @Autowired
    private HzChaBusiRMapper hzChaBusiRMapper;
    @Resource
    private IHzBusiService hzBusiService;
    @Resource
    private IHzChaChannelService hzChaChannelService;

    /**
     * 查询渠道业务关系
     *
     * @param rId 渠道业务关系主键
     * @return 渠道业务关系
     */
    @Override
    public HzChaBusiR selectHzChaBusiRByRId(Long rId) {
        HzChaBusiR hzChaBusiR = hzChaBusiRMapper.selectHzChaBusiRByRId(rId);
        hzChaBusiR.setExtend_channelName(hzChaChannelService.selectHzChaChannelByChannelId(hzChaBusiR.getChannelId()).getChannelName());
        hzChaBusiR.setExtend_busiName(hzBusiService.selectHzBusiByBusiId(hzChaBusiR.getBusiId()).getBusiName());
        if (hzChaBusiR.getFee1() != null) {
            hzChaBusiR.setExtend_fee1(hzChaBusiR.getFee1().divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toPlainString());
        }
        if (hzChaBusiR.getFee2() != null) {
            hzChaBusiR.setExtend_fee2(hzChaBusiR.getFee2().multiply(new BigDecimal(1000)).stripTrailingZeros().toPlainString());
        }
        return hzChaBusiR;
    }

    /**
     * 查询渠道业务关系列表
     *
     * @param hzChaBusiR 渠道业务关系
     * @return 渠道业务关系
     */
    @Override
    public List<HzChaBusiR> selectHzChaBusiRList(HzChaBusiR hzChaBusiR) {
        List<HzChaBusiR> hzChaBusiRList = hzChaBusiRMapper.selectHzChaBusiRList(hzChaBusiR);
        hzChaBusiRList.forEach(this::setExtendHzChaBusiR);
        return hzChaBusiRList;
    }

    /**
     * description :: 给HzChaBusiR设置扩展信息
     */
    private void setExtendHzChaBusiR(HzChaBusiR hzChaBusiR) {
        hzChaBusiR.setExtend_busiName(hzBusiService.selectHzBusiByBusiId(hzChaBusiR.getBusiId()).getBusiName());
        String extend_fee = "-";
        if (hzChaBusiR.getFeeType() == 1) {
            //单笔
            extend_fee = hzChaBusiR.getFee1().divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toPlainString();
        } else if (hzChaBusiR.getFeeType() == 2) {
            //费率
            extend_fee = hzChaBusiR.getFee2().multiply(new BigDecimal(1000)).stripTrailingZeros().toPlainString() + "‰";
        } else if (hzChaBusiR.getFeeType() == 3) {
            //单笔+费率
            extend_fee = hzChaBusiR.getFee1().divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toPlainString()
                    + " + " + hzChaBusiR.getFee2().multiply(new BigDecimal(1000)).stripTrailingZeros().toPlainString() + "‰";
        }
        hzChaBusiR.setExtend_fee(extend_fee);
        HzChaChannel hzChaChannel = hzChaChannelService.selectHzChaChannelByChannelId(hzChaBusiR.getChannelId());
        hzChaBusiR.setExtend_channelName(hzChaChannel.getChannelCode() + " :: " + hzChaChannel.getChannelName());
    }


    /**
     * 新增渠道业务关系
     *
     * @param hzChaBusiR 渠道业务关系
     * @return 结果
     */
    @Override
    public int insertHzChaBusiR(HzChaBusiR hzChaBusiR) {
        if (hzChaBusiR.getFee1() != null) {
            hzChaBusiR.setFee1(hzChaBusiR.getFee1().multiply(new BigDecimal(100)));
        }
        if (hzChaBusiR.getFee2() != null) {
            hzChaBusiR.setFee2(hzChaBusiR.getFee2().divide(new BigDecimal(1000)));
        }
        hzChaBusiR.setCreateTime(DateUtils.getNowDate());
        hzChaBusiR.setUpdateTime(DateUtils.getNowDate());
        return hzChaBusiRMapper.insertHzChaBusiR(hzChaBusiR);
    }

    /**
     * 修改渠道业务关系
     *
     * @param hzChaBusiR 渠道业务关系
     * @return 结果
     */
    @Override
    public int updateHzChaBusiR(HzChaBusiR hzChaBusiR) {
        if (hzChaBusiR.getFee1() != null) {
            hzChaBusiR.setFee1(hzChaBusiR.getFee1().multiply(new BigDecimal(100)));
        }
        if (hzChaBusiR.getFee2() != null) {
            hzChaBusiR.setFee2(hzChaBusiR.getFee2().divide(new BigDecimal(1000)));
        }
        hzChaBusiR.setUpdateTime(DateUtils.getNowDate());
        return hzChaBusiRMapper.updateHzChaBusiR(hzChaBusiR);
    }

    /**
     * 批量删除渠道业务关系
     *
     * @param rIds 需要删除的渠道业务关系主键
     * @return 结果
     */
    @Override
    public int deleteHzChaBusiRByRIds(String rIds) {
        return hzChaBusiRMapper.deleteHzChaBusiRByRIds(Convert.toStrArray(rIds));
    }

    /**
     * 删除渠道业务关系信息
     *
     * @param rId 渠道业务关系主键
     * @return 结果
     */
    @Override
    public int deleteHzChaBusiRByRId(Long rId) {
        return hzChaBusiRMapper.deleteHzChaBusiRByRId(rId);
    }
}
